package com.lw.mall.web.lw;

import com.lw.mall.entity.Product;
import com.lw.mall.entity.User;
import com.lw.mall.entity.pojo.ResultBean;
import com.lw.mall.service.ProductService;
import com.lw.mall.service2.DescImgService;
import com.lw.mall.service2.ProductService2;
import com.lw.mall.utils.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Date;

/**
 * 上架商品
 * @author liwang
 */
@Controller
@RequestMapping("/lw")
public class UpProductController {

    @Autowired
    private ProductService productService;

    @Autowired
    private ProductService2 productService2;

    @Autowired
    private DescImgService descImgService;

    /**
     * 跳转到上架商品页
     * @return
     */
    @GetMapping("/upProduct.html")
    public String upProduct(){

        return "/mall/product/upProduct";
    }

    /**
     * 商品下架
     */
    @ResponseBody
    @GetMapping("/product/downProduct")
    public ResultBean<Boolean> downProduct(@RequestParam("id") Integer id){
        productService2.downProduct(id);
        return new ResultBean<>(true);
    }

    /**
     * 在商品列表上传商品
     * @param id
     * @return
     */
    @ResponseBody
    @GetMapping("/product/upProduct")
    public ResultBean<Boolean> upProductInProductList(@RequestParam("id") Integer id){
        productService2.upProductInProductList(id);
        return new ResultBean<>(true);
    }

    //用户上架商品
    @PostMapping("/add.do")
    public void add(MultipartFile[] imageDesc,MultipartFile image,
                    String title,
                    Double marketPrice,
                    Double shopPrice,
                    String desc,
                    int csid,
                    HttpServletRequest request,
                    HttpServletResponse response) throws Exception {
        User user = (User) request.getSession().getAttribute("user");
        Product product = new Product();
        product.setSellId(user.getId());
        product.setTitle(title);
        product.setMarketPrice(new BigDecimal(marketPrice));
        product.setShopPrice(new BigDecimal(shopPrice));
        product.setDesc(desc);
        product.setIsHot(0);//默认为0不热门
        product.setCsid(csid);
        product.setPdate(new Date());
        String imgUrl = FileUtil.saveFileAliyun(image);
        product.setImage(imgUrl);
        product.setIsDown(0);//默认为不下架
        int id = productService.create(product);
        //描述图片
        descImgService.saveDescImg(imageDesc,product.getId());
        if (id <= 0) {
            request.setAttribute("message", "添加失败！");
            request.getRequestDispatcher("toAdd.html").forward(request, response);
        } else {
            response.sendRedirect("/mall");
        }
    }
}
